#ifndef _BUFFER_H
#define _BUFFER_H
#include <stdlib.h>

struct buffer {
	char* data;
	u_int32_t length;
	u_int32_t limit;
	u_int32_t index;
	struct buffer *next;
	u_int32_t formatPos;
	u_int32_t formatSize;
};

struct bufferFactory {
	struct obj_pool *buffer_pool;
	struct obj_pool *bytes_pool;
};
extern struct bufferFactory* bufferFactory_build(size_t buffer_len,
		size_t count);
extern int bufferFactory_release(struct bufferFactory *factory);

extern struct buffer* buffer_new(struct bufferFactory *factory);
extern int buffer_del(struct bufferFactory *factory, struct buffer *buffer);

/**
 * write data to file(socket)
 * change limit before calling
 **/
extern void ready_to_write(struct buffer *buff);
/**
 * read data from file (socket)
 * change limit after calling
 **/
extern void ready_to_read(struct buffer *buff);

extern int bytes_to_byte(struct buffer *pinput, char *value, int is_big);
extern int byte_to_bytes(struct buffer *poutput, char value, int is_big);

extern int bytes_to_short(struct buffer *pinput, u_int16_t *value, int is_big);
extern int short_to_bytes(struct buffer *poutput, u_int16_t value, int is_big);

extern int bytes_to_int(struct buffer *pinput, u_int32_t *value, int is_big);
extern int int_to_bytes(struct buffer *poutput, u_int32_t value, int is_big);

extern int bytes_to_str(struct buffer *pinput, char **value, int is_big);
extern int str_to_bytes(struct buffer *poutput, char *value, int is_big);

extern int buffer_skip(struct buffer *pbuffer, u_int32_t count);

extern int buffer_begin_batch(struct buffer *pbuffer, u_int32_t formatSize);
extern int buffer_end_batch(struct buffer *pbuffer, u_int32_t batchCount,
		int is_big);

extern int bytes_to_barray(struct buffer *pinput, char *value, size_t arr_len);
extern int barray_to_bytes(struct buffer *poutput, char *value, size_t arr_len);

extern void buffer_compact(struct buffer *buff);
#endif
